package com.zhss.dfs.datanode.server;

import java.util.concurrent.CountDownLatch;

/**
 * @author: zcl
 * @date: 2021/1/27 20:11
 * @description:  负责跟一组NameNode 通讯的组件
 */
public class NameNodeGroupOfferService {

    /**
     * 负责和master NameNode通讯的组件
     */
    NameNodeServiceActor masterNameNodeServiceActor;

    /**
     * 负责和slave NameNodeService通讯的组件
     */
    NameNodeServiceActor slaveNameNodeServiceActor;

    public NameNodeGroupOfferService() {
        this.masterNameNodeServiceActor = new NameNodeServiceActor(true);
        this.slaveNameNodeServiceActor = new NameNodeServiceActor(false);
    }

    /**
     * 开始执行
     * @param dataNode
     */
    public void start(DataNode dataNode) {
        register(dataNode);
    }

    /**
     * 完成dataNode 向mater和slave DataNode注册的两步操作
     * @param dataNode
     */
    private void register(DataNode dataNode) {
        try {
            CountDownLatch latch=new CountDownLatch(2);
            masterNameNodeServiceActor.register(latch,dataNode);
            slaveNameNodeServiceActor.register(latch,dataNode);
            latch.await();
        }catch (Exception e){
            System.out.println(e.toString());
        }
    }
}
